package barbershop.com.controller.orders;

import barbershop.com.entity.Vip;
import barbershop.com.entity.orders.OrderItem;
import barbershop.com.entity.orders.Orders;
import barbershop.com.entity.orders.Param;
import barbershop.com.service.VipService;
import barbershop.com.service.orders.OrderItemService;
import barbershop.com.service.orders.OrderService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private VipService vipService;

    // 获取所有订单条目
    @RequestMapping("/findAll")
    public List<Orders> findAll(){
        return this.orderService.findAll();
    }

    // 获取某订单下的所有订单条目
    @RequestMapping("/findAllItems")
    public List<OrderItem> findAllItems(@RequestParam(required = false)Integer orderId){
        return this.orderService.findAllOrderItems(orderId);
    }

    // 销售记录，将本次订单插入数据库
    @RequestMapping("/invoiceAccount")
    public Orders invoiceAccount(@RequestBody(required = false)Param param){
        // 新建一个order
        Orders order = this.orderService.insert(param.order);
        // 金额小计，只对前端传来的值进行计算，而不是先插入数据再计算字段“小计”的值
        this.orderItemService.calAllSubTotalMoney(param.orderItemList);
        // 更新orderItem表
        for (OrderItem orderItem : param.orderItemList){ orderItem.setOrders(order); }
        this.orderItemService.updateOrderItem(param.orderItemList);

        // 更新order表，计算总金额和总数量
        Integer orderId = order.getOrderId();
        this.orderService.updateTotalQP(orderId);

        return order;
    }

    // 获取某订单中的订单总条目及总价
    @RequestMapping("/getTotalQP")
    public Object getTotalQP(@RequestParam(required = false)Integer orderId){
        JSONObject object = new JSONObject();
        Integer totalQuantity = this.orderService.getTotalQuantity(orderId);
        Double totalPrice = this.orderService.getTotalPrice(orderId);
        object.put("totalQuantity", totalQuantity);
        object.put("totalPrice", totalPrice);
        System.out.println("该订单条目总数目为"+totalQuantity+", 总价是"+totalPrice+"元");
        return object;
    }

    // 查找所有交易成功的订单
    @RequestMapping("/getSuccessOrder")
    public List<Orders> getSuccessOrder(){
        return this.orderService.getSuccessOrder(1);
    }

    /** CRUD **/
    @RequestMapping("/add")
    public Orders addOrder(@RequestBody Orders record){
        return this.orderService.insert(record);
    }

    @RequestMapping("/delete")
    public Boolean deleteOrder(@RequestBody Orders record){
        return this.orderService.delete(record);
    }

    @RequestMapping("/update")
    public Orders updateOrder(@RequestBody Orders record){ return this.orderService.update(record); }

    @RequestMapping("/find")
    public Orders findOrder(@RequestBody Orders record){
        return this.orderService.find(record);
    }
}

